package utils;



import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class LineRegex {
	
	static Pattern code_pattern = Pattern.compile("&lt;code&gt;(.*?)&lt;/code&gt;");
	static Pattern body_pattern = Pattern.compile("Body=\"(.*?)\" OwnerUserId=\"");
	static Pattern text_pattern = Pattern.compile("&lt;/code&gt;(.*?)&lt;code&gt;");
	static Pattern word_pattern = Pattern.compile("[A-Za-z\\-#+']++");
	static Pattern tags_pattern = Pattern.compile("Tags=\"(.*?)\"");
	static Pattern taggs_pattern = Pattern.compile("&lt;(.*?)&gt;");
	static Pattern score_pattern = Pattern.compile("Score=\"(.*?)\"");
	static Pattern view_count_pattern = Pattern.compile("ViewCount=\"(.*?)\"");
	static Pattern user_id_pattern = Pattern.compile("OwnerUserId=\"(.*?)\"");
	static Pattern title_pattern = Pattern.compile("Title=\"(.*?)\"");
		
	public static String getBody(String line) {
		Matcher body_matcher = body_pattern.matcher(line);
		if ( ! body_matcher.find() ) {
			return "";
		}
		String body = body_matcher.group(1);
		body = body.replaceAll("&lt;.{1,8}&gt;", "").replaceAll("&.{1,4};", "");
		return body;
	}
	
	/**
	 * the line can be text.
	 * If you want the words in the body of a question you need to call getBody first
	 * then pass the body to this function
	 * @param text
	 * @return
	 */
	public static ArrayList<String> getWords(String text) {
		Matcher word_matcher = word_pattern.matcher(text);
		ArrayList<String> res = new ArrayList<String>();
		while ( word_matcher.find() ) {
			String word = word_matcher.group().toLowerCase();
			char c = word.charAt(word.length()-1);
			if ( c == '-' ) word = word.substring(0,word.length()-1); 
			res.add(word);
		}
		return res;
	}
	
	public static String getTitle(String line) {
		Matcher title_matcher = title_pattern.matcher(line);
		title_matcher.find();
		String title = title_matcher.group(1);
		return title;
	}
	
	public static int getUserId(String line) {
		Matcher user_id_matcher = user_id_pattern.matcher(line);
		if( ! user_id_matcher.find() ) return -1;
		String user_id = user_id_matcher.group(1);
		return Integer.parseInt(user_id);
	}
	
	public static ArrayList<String> getRelevantTags(String line,TagClustering tag_clustering) {
		ArrayList<String> res = new ArrayList<String>();
		for ( String tag : getAllTags(line) ) {
			if ( tag_clustering.isRelevant(tag) )
				res.add(tag);
		}
		return res;
	}
	
	public static ArrayList<String> getAllTags(String line) {
		ArrayList<String> res = new ArrayList<String>();
		Matcher m = tags_pattern.matcher(line);
		m.find();
		String all_taggs = m.group(1);
		Matcher mm = taggs_pattern.matcher(all_taggs);
		while ( mm.find() ) {
			String tag = mm.group(1);
			res.add(tag);
		}
		return res;
	}
	
	public static ArrayList<String> getClusters(String line,TagClustering tag_clustering) {
		ArrayList<String> res = new ArrayList<String>();
		for ( String tag : getRelevantTags(line,tag_clustering) ) {
			String cluster = tag_clustering.getClusterForTag(tag).toString();
			if ( ! res.contains(cluster) )
				res.add(cluster);
		}
		return res;
	}
	
	public static int getViewCount(String line) {
		Matcher view_count_matcher = view_count_pattern.matcher(line);
		view_count_matcher.find();
		String view_count = view_count_matcher.group(1);
		return Integer.parseInt(view_count);
	}
	
	public static int getScore(String line) {
		Matcher score_matcher = score_pattern.matcher(line);
		score_matcher.find();
		String score = score_matcher.group(1);
		return Integer.parseInt(score);
	}
	

}
